// pages/personal/personal.js
import request from "../../utils/request";
let touchStarY
let touchEndY
let moveDistance
Page({

  /**
   * 页面的初始数据
   */
  data: {
    coverTransform: 'translateY(0)',
    coverTransition: '',
    userInfo:{
      avatarUrl:"/static/images/personal/missing-face.png",
      nickname:"游客"
    },
    recentPlayList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let userInfo = JSON.parse(wx.getStorageSync("userInfo") || null)
    if(userInfo){
      this.setData({
        userInfo
      })
      this.getUserRecentPlayList(userInfo.userId)
    }
  },
  async getUserRecentPlayList(userId){
    let recentPlayListData = await request("/user/record", {uid:userId, type:0})
    if(recentPlayListData.code==200){
      this.setData({
        recentPlayList : recentPlayListData.allData.slice(0, 10).map(e =>{
          e.id = e.song.id
          return e
        })
      })
    }
  },

  handleTouchStart(event){
    this.setData({
      coverTransition:``
    })
    touchStarY = event.touches[0].clientY
  },
  handleTouchMove(event){
    moveDistance = event.touches[0].clientY - touchStarY
    if(moveDistance <= 0){
      return
    }
    if(moveDistance >= 80){
      moveDistance = 80
    }
    this.setData({
      coverTransform: `translateY(${moveDistance}rpx)`
    })
  },
  handleTouchEnd(event){
    this.setData({
      coverTransform: `translateY(0)`,
      coverTransition: `transform .5s linear`
    })
  },
  toLogin:function (){
    wx.navigateTo({url:"/pages/login/login"})
  },
  collectSongList(){
    wx.navigateTo({
      url:"/songPackage/pages/collectSong/collectSong"
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})